package uebung1;

public class U1A1A2 {
	public static void main(String[] args) {
		MyVector<String> v = new MyVector<String>();
		v.push_back(new String("Hund"));
		v.push_front("Katze");
		v.push_front("Schlange");
		v.push_back("Vogel");
		v.push_back(new String("Hund"));
		v.print();
		System.out.println("Done.");
	}
}

interface SimpleCollection<T> {
	public int size();

	public T get(int position);

	public void set(int position, T data);

	public void push_front(T data);

	public void push_back(T data);

	public void delete(int position);

	public void print();
}

class MyVector<T> implements SimpleCollection<T> {
	T[] m_elements;
	int m_nextFree, m_first;
	int capacityIncrement;

	MyVector(int initialcapacity, int capacityIncrement) {
		m_elements = (T[]) new Object[initialcapacity];
		m_nextFree = 0;
		m_first = 0;
	}

	MyVector(int initialcapacity) {
		
		this(initialcapacity, 0);
	}

	MyVector() {
		this(1, 0);
	}

	private void resize_left() {
		System.out.println("Resizing left...");
		int increment = capacityIncrement == 0 ? m_elements.length : capacityIncrement;
		T[] temp = (T[]) new Object[m_elements.length+increment];
		for (int i = m_first; i < m_nextFree; ++i) {
			temp[i+increment] = m_elements[i];
		}
		m_nextFree=m_nextFree+increment;
		m_first=m_first+increment;
		m_elements=temp;
	}

	private void resize_right() {
		System.out.println("Resizing right");
		int increment = capacityIncrement == 0 ? m_elements.length : capacityIncrement;
		T[] temp = (T[]) new Object[m_elements.length+increment];
		for (int i = m_first; i < m_nextFree; ++i) {
			temp[i] = m_elements[i];
		}
		m_elements=temp;
	}

	@Override
	public int size() {
		return m_nextFree-m_first;
	}
	
	//Capacity

	@Override
	public T get(int position) {
		return m_elements[m_first+position];
	}

	@Override
	public void set(int position, T data) {
		m_elements[m_first+position] = data;

	}

	@Override
	public void push_front(T data) {
		System.out.println("m_prevFree="+m_first+" length="+m_elements.length+"m_nextFree="+m_nextFree);
		if(m_first==0)
			resize_left();
		m_elements[m_first-1]=data;
		--m_first;
	}

	@Override
	public void push_back(T data) {
		System.out.println("m_NextFree="+m_nextFree+" length="+m_elements.length);
		if(m_nextFree>=m_elements.length) //geht auch ==
			resize_right();
		m_elements[m_nextFree++]=data;
	}

	@Override
	public void delete(int position) {
		
	}

	@Override
	public void print() {
		for (int i = 0; i < m_elements.length; ++i)
			System.out.println(m_elements[i] + " ");
	}
}
